package httpServer

import (
	"GinTemplate/utils/formatTime"
	"GinTemplate/utils/waitGroup"
	"context"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

var wg waitGroup.WaitGroupWapper

func NewServer(host string, port string, handler http.Handler) *http.Server {
	var addr string
	if host == "" || port == "0" {
		addr = "127.0.0.1:8080"
	} else {
		addr = fmt.Sprintf("%s:%s", host, port)
	}
	return &http.Server{
		Addr:    addr,
		Handler: handler,
	}
}

func ListenAndServe(server *http.Server) {
	wg.Go(func() {
		err := server.ListenAndServe()
		if err != nil && err != http.ErrServerClosed {
			panic(err)
		}
		fmt.Println("Server shutdown at", time.Now().Format(formatTime.TimeFullFormat))
	})
}

type AfterCloseHandler func()

// 监听linux信号，收到信号，停止服务
func WaitForShutdown(server *http.Server, handlers ...AfterCloseHandler) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT)
	sig := <-c
	switch sig {
	case syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT:
		ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
		defer cancelFunc()
		if err := server.Shutdown(ctx); err != nil {
			fmt.Printf("An error occurs when Server shutdown:%v", err)
		}
		for _, handler := range handlers {
			handler()
		}
	}
	wg.Wait()
}
